package com.krtech.wecard.module.ramble.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.krtech.wecard.framework.exception.BusinessException;
import com.krtech.wecard.framework.utils.LoginUtil;
import com.krtech.wecard.module.ramble.entity.*;
import com.krtech.wecard.module.ramble.enums.RambleEnums;
import com.krtech.wecard.module.ramble.service.RambleAcceptService;
import com.krtech.wecard.module.ramble.service.RambleDrawLogService;
import com.krtech.wecard.module.ramble.service.RambleKanjiUrlService;
import com.krtech.wecard.module.sys.entity.SysKeyValue;
import com.krtech.wecard.module.sys.entity.SysUser;
import com.krtech.wecard.module.sys.service.SysKeyValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rambleAccept")
@Slf4j
@Api(tags = "佳得乐漫步-领取记录")
public class RambleAcceptController {

    @Autowired
    private RambleAcceptService acceptService;

    @Autowired
    private RambleDrawLogService drawLogService;

    @Autowired
    private SysKeyValueService keyValueService;

    @Autowired
    private RambleKanjiUrlService kanjiUrlService;

    @GetMapping("/getUrlByKanji")
    @ApiOperation(value = "获得领取地址")
    public RambleKanjiUrl getUrlByKanji(Integer kanji) {
        String today = DateUtil.today();
        RambleKanjiUrl kanjiUrl = kanjiUrlService.getOne(new QueryWrapper<RambleKanjiUrl>()
                .eq(RambleKanjiUrl.COL_KANJI, kanji).eq(RambleKanjiUrl.COL_KANJI_DATE, today));
        return kanjiUrl;
    }

    @PostMapping("/save")
    @ApiOperation(value = "save")
    public boolean save(@RequestBody RambleKanjiUrl kanji) {
        kanji.setCreatedTime(new Date());
        return kanjiUrlService.save(kanji);
    }

    @GetMapping("/getMyAccept")
    @ApiOperation(value = "获得我今日的领取信息")
    public List<RambleAcceptVo> getMyAccept() {
        SysUser userInfo = LoginUtil.getUserInfo();
        String today = DateUtil.today();

        //查询环境变量
        SysKeyValue stepKeyValue = keyValueService.getOne(new QueryWrapper<SysKeyValue>()
                .eq(SysKeyValue.COL_SYS_NAME, RambleEnums.STEP));

        if (stepKeyValue == null){
            throw new BusinessException("环境变量step未配置，请联系管理员");
        }

        int step = Integer.parseInt(stepKeyValue.getSysValue());


//        List<RambleDrawLog> drawLogList = drawLogService.list(new QueryWrapper<RambleDrawLog>()
//                .eq(RambleDrawLog.COL_PHONE, userInfo.getPhone()).eq(RambleDrawLog.COL_DRAW_TIME, today));
//
//        if (drawLogList.size() <= 0){
//            return null;
//        }

        List<RambleAccept> acceptList = acceptService.list(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_PHONE, userInfo.getPhone()).eq(RambleAccept.COL_ACCEPT_DATE, today));

        //计算当日已领取
        int sum = drawLogService.getCanAcceptCount();

        sum = sum + acceptList.size();

        List<RambleAcceptVo> acceptVoArrayList = new ArrayList<>();

        for (int i = 1;i <= sum ; i++){
            Integer queryKanjiStep = i * step;
            List<RambleAccept> judgeHasAccept = acceptList.stream()
                    .filter(one -> one.getKanji().equals(queryKanjiStep)).collect(Collectors.toList());

            RambleAcceptVo acceptVo = new RambleAcceptVo();
            acceptVo.setKanjiStep(queryKanjiStep);
            if (judgeHasAccept.size() > 0){
                //如果有，就说明已经领取了
                acceptVo.setHasDraw(1);
            }else {
                acceptVo.setHasDraw(0);
            }
            acceptVoArrayList.add(acceptVo);
        }
        return acceptVoArrayList;
    }

    @PostMapping("/accept")
    @ApiOperation(value = "领取")
    public boolean getLogInfo(@RequestBody RambleAccept accept) {
        SysUser userInfo = LoginUtil.getUserInfo();

        if (accept.getKanji() == null){
            throw new BusinessException("必填参数坎级不能为空，就是领取的步数");
        }
        //判断是否领取过了
        int judgeAccept = acceptService.count(new QueryWrapper<RambleAccept>()
                .eq(RambleAccept.COL_PHONE, userInfo.getPhone())
                .eq(RambleAccept.COL_KANJI, accept.getKanji())
                .eq(RambleAccept.COL_ACCEPT_DATE,DateUtil.today()));

        if (judgeAccept > 0){
//            throw new BusinessException("当前奖励已经领取，请多走一会再来领取哦~");
            return false;
        }

        accept.setCreatedTime(new Date());
        accept.setAcceptDate(new Date());
        accept.setNickName(userInfo.getNickName());
        accept.setPhone(userInfo.getPhone());
        accept.setOpenId(userInfo.getMipOpenId());

        return acceptService.save(accept);
    }

    @GetMapping("/myVolume")
    @ApiOperation(value = "我领取的卷")
    public List<RambleAccept> myVolume() {
        SysUser userInfo = LoginUtil.getUserInfo();
        return acceptService.list(new QueryWrapper<RambleAccept>().eq(RambleAccept.COL_PHONE,userInfo.getPhone()));
    }
}
